package com.example.springbootfenye.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springbootfenye.entity.UserEntity;
import com.example.springbootfenye.mapper.UserMapper;
import com.example.springbootfenye.req.UserReq;
import com.example.springbootfenye.resp.CommonResp;
import com.example.springbootfenye.resp.PageResp;
import com.example.springbootfenye.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
public class UserServiceimpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {
    @Autowired
    private UserMapper usermapper;
    @Override
    public PageResp<UserEntity> getList(UserReq userReq) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<UserEntity>();
        if(!ObjectUtils.isEmpty(userReq.getName())){
            queryWrapper.lambda().eq(UserEntity::getName,userReq.getName());
        }
        if(!ObjectUtils.isEmpty(userReq.getPhone())){
            queryWrapper.lambda().eq(UserEntity::getPhone,userReq.getPhone());
        }
        if(!ObjectUtils.isEmpty(userReq.getCity())){
            queryWrapper.lambda().eq(UserEntity::getCity,userReq.getCity());
        }
        Page<UserEntity> page = new Page<>(userReq.getPage(),userReq.getSize());
        IPage<UserEntity> userEntityIPage = usermapper.selectPage(page,queryWrapper);
        PageResp<UserEntity> pageResp = new PageResp<>();
        pageResp.setTotal(userEntityIPage.getTotal());
        pageResp.setList(userEntityIPage.getRecords());
        return pageResp;
    }
}
